# Petit FatFs

Library for MSP430G2553 that helps to interact with SD cards with the FAT file system.

This is port of [Petit FatFs module (0.03a)](http://elm-chan.org/fsw/ff/arc/pff3a.zip) with applied [patch](http://elm-chan.org/fsw/ff/patch/pff3a_p1.diff).
All functions that are hard-conected to hardware are located in spi.c file. If you want to [port](https://notabug.org/dh33ex/pffs/src/master/PORTING.md) this library for your MSP430 model (or maybe for another general-purpose MCU), you need to edit this file.
By default, code uses B0 USI module and P1.3 as CS.

## Examples

### File read
```
#include <msp430g2553.h>
#include "pff.h"

int main(void) {
    WDTCTL = WDTPW + WDTHOLD;
    P1DIR |= BIT0;                  // set pin to output
    P1OUT &= ~BIT0;                 // disable LED initially
    
    FATFS fs;                       // Work area (file system object) for the volume
    BYTE buff[16];                  // File read buffer
    UINT br;                        // File read count
    FRESULT res;                    // Petit FatFs function common result code
    
    // Mount the volume
    res = pf_mount(&fs);
    
    if (res != FR_OK) {
        while (1);                  // don't continue after an error
    }
    
    // Open a file
    res = pf_open("/TEST.TXT");
    
    if (res != FR_OK) {
        while (1);                  // don't continue after an error
    }
    
    // Read data to the memory
    res = pf_read(buff, 16, &br);   // read 16 bytes from file
    
    P1OUT |= BIT0;                  // enable LED on finish
    while (1);
    
    return 0;
}
```

### File write
```
#include <msp430g2553.h>
#include <string.h>
#include "pff.h"

int main(void) {
    WDTCTL = WDTPW + WDTHOLD;
    P1DIR |= BIT0;                  // set pin to output
    P1OUT &= ~BIT0;                 // disable LED initially
    
    FATFS fs;                       // Work area (file system object) for the volume
    BYTE *buff = "test";            // File write buffer
    UINT bw;                        // File write count
    FRESULT res;                    // Petit FatFs function common result code
    
    // Mount the volume
    res = pf_mount(&fs);
    
    if (res != FR_OK) {
        while (1);                  // don't continue after an error
    }
    
    // Open a file
    res = pf_open("/TEST.TXT");
    
    if (res != FR_OK) {
        while (1);                  // don't continue after an error
    }
    pf_write(buff, 5, &bw);         // initiate write operation, write data to the file
    pf_write(0, 0, &bw);            // finalize the current write operation.
    
    P1OUT |= BIT0;                  // enable LED on finish
    while (1);
    
    return 0;
}
```

## License

### Original

> /*----------------------------------------------------------------------------/  
> /  Petit FatFs - FAT file system module  R0.03a  
> /-----------------------------------------------------------------------------/  
> 
> Copyright (C) 2019, ChaN, all right reserved.  
> 
> Petit FatFs module is an open source software. Redistribution and use of  
> Petit FatFs in source and binary forms, with or without modification, are  
> permitted provided that the following condition is met:  
>   
> 1. Redistributions of source code must retain the above copyright notice,  
>    this condition and the following disclaimer.  
> 
> This software is provided by the copyright holder and contributors "AS IS"  
> and any warranties related to this software are DISCLAIMED.  
> The copyright owner or contributors be NOT LIABLE for any damages caused  
> by use of this software.  



### Current
GNU GPLv3 license (see LICENSE file for details).


## Sources used during development

[Original ChaN’s Petit Fat FileSystem module](http://elm-chan.org/fsw/ff/00index_p.html)  
[Port of version R0.03 by RickKimbal](https://gist.github.com/RickKimball/2325039)  
[rjhcoding’s tutorial about interface of the SD card](http://www.rjhcoding.com/avrc-sd-interface-1.php)  
